<template>
  <div class="flow-flowModel-container">
    <el-card shadow="hover">
        <div class="flow-flowModel-search mb15">
            <el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
            <el-row>
                <el-col :span="8" class="colBlock">
                  <el-form-item label="流程名称" prop="name">
                    <el-input
                        v-model="tableData.param.name"
                        placeholder="请输入流程名称"
                        clearable
                        @keyup.enter.native="flowModelList"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8" class="colBlock">
                  <el-form-item label="表单类型" prop="flowType">
                    <el-select v-model="tableData.param.flowType" placeholder="请选择表单类型" clearable style="width: 160px">
                        <el-option
                            v-for="dict in flow_model_type"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                        />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8" :class="!showAll ? 'colBlock' : 'colNone'">
                  <el-form-item>
                    <el-button type="primary"  @click="flowModelList"><el-icon><ele-Search /></el-icon>搜索</el-button>
                    <el-button  @click="resetQuery(queryRef)"><el-icon><ele-Refresh /></el-icon>重置</el-button>
                    <el-button type="primary" link  @click="toggleSearch">
                      {{ word }}
                      <el-icon v-show="showAll"><ele-ArrowUp/></el-icon>
                      <el-icon v-show="!showAll"><ele-ArrowDown /></el-icon>
                    </el-button>
                  </el-form-item>
                </el-col>
                <el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
                  <el-form-item label="流程表单类别" prop="flowFormCate">
                    <el-select v-model="tableData.param.flowFormCate" placeholder="请选择流程表单类别" clearable  >
                      <el-option
                          v-for="item in flowFormCateOptions"
                          :key="item.key"
                          :label="item.value"
                          :value="item.key"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
                  <el-form-item label="业务表单类别" prop="flowBusinessCate">
                    <el-select v-model="tableData.param.flowBusinessCate" placeholder="请选择业务表单类别" clearable >
                        <el-option
                            v-for="dict in flow_model_cate"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                        />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
                  <el-form-item label="排序" prop="sort">
                    <el-select v-model="tableData.param.sort" placeholder="请选择排序" clearable >
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
                  <el-form-item label="流程状态" prop="status">
                    <el-select v-model="tableData.param.status" placeholder="请选择流程状态" clearable >
                        <el-option
                            v-for="dict in flow_form_status"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                        />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
                  <el-form-item>
                    <el-button type="primary"  @click="flowModelList"><el-icon><ele-Search /></el-icon>搜索</el-button>
                    <el-button  @click="resetQuery(queryRef)"><el-icon><ele-Refresh /></el-icon>重置</el-button>
                    <el-button type="primary" link  @click="toggleSearch">
                        {{ word }}
                        <el-icon v-show="showAll"><ele-ArrowUp/></el-icon>
                        <el-icon v-show="!showAll"><ele-ArrowDown /></el-icon>
                    </el-button>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  @click="handleAdd"
                  v-auth="'api/v1/flow/flowModel/add'"
                ><el-icon><ele-Plus /></el-icon>新增</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="success"
                  :disabled="single"
                  @click="handleUpdate(null)"
                  v-auth="'api/v1/flow/flowModel/edit'"
                ><el-icon><ele-Edit /></el-icon>修改</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="danger"
                  :disabled="multiple"
                  @click="handleDelete(null)"
                  v-auth="'api/v1/flow/flowModel/delete'"
                ><el-icon><ele-Delete /></el-icon>删除</el-button>
              </el-col>
             <el-col :span="1.5">
                <el-button
                        type="warning"
                        @click="handleExport()"
                        v-auth="'api/v1/flow/flowModel/export'"
                ><el-icon><ele-Download /></el-icon>导出Excel</el-button>
             </el-col>
                <el-col :span="1.5">
                    <el-button
                            type="success"
                            @click="handleImport()"
                            v-auth="'api/v1/flow/flowModel/import'"
                    ><el-icon><ele-Upload /></el-icon>导入Excel</el-button>
                </el-col>
            </el-row>
        </div>
        <el-table v-loading="loading" :data="tableData.data" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="主键" align="center" prop="id"
            min-width="150px"
             />
          <el-table-column label="流程名称" align="center" prop="name"
            min-width="150px"
             />
          <el-table-column label="表单类型" align="center" prop="flowType" :formatter="flowTypeFormat"
            min-width="150px"
             />
          <el-table-column label="流程表单类别" align="center" prop="linkedFlowFormCate.name"
            min-width="150px"
             />
          <el-table-column label="业务表单类别" align="center" prop="flowBusinessCate" :formatter="flowBusinessCateFormat"
            min-width="150px"
             />
          <el-table-column label="流程状态" align="center" prop="status" :formatter="statusFormat"
            min-width="150px"
             />
          <el-table-column label="创建时间" align="center" prop="createdAt"
            min-width="150px"
            >
            <template #default="scope">
                <span>{{ proxy.parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建人" align="center" prop="createdBy"
            min-width="150px"
             />
          <el-table-column label="操作" align="center" class-name="small-padding" min-width="200px" fixed="right">
            <template #default="scope">
              <el-button
                type="primary"
                link
                @click="handleView(scope.row)"
                v-auth="'api/v1/flow/flowModel/get'"
              ><el-icon><ele-View /></el-icon>详情</el-button>
              <el-button
                type="primary"
                link
                @click="handleUpdate(scope.row)"
                v-auth="'api/v1/flow/flowModel/edit'"
              ><el-icon><ele-EditPen /></el-icon>修改</el-button>
              <el-button
                  type="primary"
                  link
                  @click="handleDesign(scope.row)"
                  v-auth="'api/v1/flow/flowModel/design'"
              ><el-icon><ele-Notification /></el-icon>流程设计</el-button>
              <el-button
                type="primary"
                link
                @click="handleDelete(scope.row)"
                v-auth="'api/v1/flow/flowModel/delete'"
              ><el-icon><ele-DeleteFilled /></el-icon>删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
            v-show="tableData.total>0"
            :total="tableData.total"
            v-model:page="tableData.param.pageNum"
            v-model:limit="tableData.param.pageSize"
            @pagination="flowModelList"
        />
    </el-card>
    <ApiV1FlowFlowModelEdit
       ref="editRef"
       :flowTypeOptions="flow_model_type"
       :flowFormCateOptions="flowFormCateOptions"
       :flowBusinessCateOptions="flow_model_cate"
       :statusOptions="flow_form_status"
       @flowModelList="flowModelList"
    ></ApiV1FlowFlowModelEdit>
    <ApiV1FlowFlowModelDetail
      ref="detailRef"
      :flowTypeOptions="flow_model_type"
      :flowFormCateOptions="flowFormCateOptions"
      :flowBusinessCateOptions="flow_model_cate"
      :statusOptions="flow_form_status"
      @flowModelList="flowModelList"
    ></ApiV1FlowFlowModelDetail>
    <loadExcel ref="loadExcelFlowModelRef" @getList="flowModelList"
               upUrl="api/v1/flow/flowModel/import"
               tplUrl="/api/v1/flow/flowModel/excelTemplate"></loadExcel>
  </div>
</template>
<script lang="ts">
import {ItemOptions} from "/@/api/items";
import {toRefs, reactive, onMounted, ref, defineComponent, computed,getCurrentInstance,toRaw} from 'vue';
import {ElMessageBox, ElMessage, FormInstance} from 'element-plus';
import {
    listFlowModel,
    delFlowModel,
    linkedDataSearch
} from "/@/api/flow/flowModel";
import {
    FlowModelTableColumns,
    FlowModelInfoData,
    FlowModelTableDataState,
} from "/@/views/flow/flowModel/list/component/model"
import ApiV1FlowFlowModelEdit from "/@/views/flow/flowModel/list/component/edit.vue"
import ApiV1FlowFlowModelDetail from "/@/views/flow/flowModel/list/component/detail.vue"
import {downLoadXml} from "/@/utils/zipdownload";
import loadExcel from "/@/components/loadExcel/index.vue"
import {useRouter} from "vue-router";
export default defineComponent({
    name: "ApiV1FlowFlowModelList",
    components:{
        loadExcel,
        ApiV1FlowFlowModelEdit,
        ApiV1FlowFlowModelDetail
    },
    setup() {
        const router = useRouter();
        const {proxy} = <any>getCurrentInstance()
        const loading = ref(false)
        const queryRef = ref()
        const editRef = ref();
        const detailRef = ref();
        const loadExcelFlowModelRef = ref();
        // 是否显示所有搜索选项
        const showAll =  ref(false)
        // 非单个禁用
        const single = ref(true)
        // 非多个禁用
        const multiple =ref(true)
        const word = computed(()=>{
            if(showAll.value === false) {
                //对文字进行处理
                return "展开搜索";
            } else {
                return "收起搜索";
            }
        })
        // 字典选项数据
        const {
            flow_model_type,
            flow_model_cate,
            flow_form_status,
        } = proxy.useDict(
            'flow_model_type',
            'flow_model_cate',
            'flow_form_status',
        )
        // flowFormCateOptions关联表数据
        const flowFormCateOptions = ref<Array<ItemOptions>>([])
        const state = reactive<FlowModelTableDataState>({
            ids:[],
            tableData: {
                data: [],
                total: 0,
                loading: false,
                param: {
                    pageNum: 1,
                    pageSize: 10,
                    name: undefined,
                    flowType: undefined,
                    flowFormCate: undefined,
                    flowBusinessCate: undefined,
                    sort: undefined,
                    status: undefined,
                    dateRange: []
                },
            },
        });
        // 页面加载时
        onMounted(() => {
            initTableData();
        });
        // 初始化表格数据
        const initTableData = () => {
            linkedData()
            flowModelList()
        };
        const linkedData = ()=>{
            linkedDataSearch().then((res:any)=>{
                //关联flow_form表选项
                flowFormCateOptions.value = proxy.setItems(res, 'id', 'name','linkedFlowModelFlowForm')
            })
        }
        /** 重置按钮操作 */
        const resetQuery = (formEl: FormInstance | undefined) => {
            if (!formEl) return
            formEl.resetFields()
            flowModelList()
        };
        // 获取列表数据
        const flowModelList = ()=>{
          loading.value = true
          listFlowModel(state.tableData.param).then((res:any)=>{
            let list = res.data.list??[];
            list.map((item:any)=>{
                item.createdBy = item.createdUser?.userNickname
            })
            state.tableData.data = list;
            state.tableData.total = res.data.total;
            loading.value = false
          })
        };
        const toggleSearch = () => {
            showAll.value = !showAll.value;
        }
        // 表单类型字典翻译
        const flowTypeFormat = (row:FlowModelTableColumns) => {
            return proxy.selectDictLabel(flow_model_type.value, row.flowType);
        }
        // 业务表单类别字典翻译
        const flowBusinessCateFormat = (row:FlowModelTableColumns) => {
            return proxy.selectDictLabel(flow_model_cate.value, row.flowBusinessCate);
        }
        // 流程状态字典翻译
        const statusFormat = (row:FlowModelTableColumns) => {
            return proxy.selectDictLabel(flow_form_status.value, row.status);
        }
        // 多选框选中数据
        const handleSelectionChange = (selection:Array<FlowModelInfoData>) => {
            state.ids = selection.map(item => item.id)
            single.value = selection.length!=1
            multiple.value = !selection.length
        }
        const handleAdd =  ()=>{
            editRef.value.openDialog()
        }
        const handleUpdate = (row: FlowModelTableColumns) => {
            if(!row){
                row = state.tableData.data.find((item:FlowModelTableColumns)=>{
                    return item.id ===state.ids[0]
                }) as FlowModelTableColumns
            }
            editRef.value.openDialog(toRaw(row));
        };
        const handleDesign = (row :FlowModelTableColumns) =>{
          router.push('/flow/flowModel/design?id='+row.id)
        }
        const handleDelete = (row: FlowModelTableColumns) => {
            let msg = '你确定要删除所选数据？';
            let id:number[] = [] ;
            if(row){
            msg = `此操作将永久删除数据，是否继续?`
            id = [row.id]
            }else{
            id = state.ids
            }
            if(id.length===0){
                ElMessage.error('请选择要删除的数据。');
                return
            }
            ElMessageBox.confirm(msg, '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    delFlowModel(id).then(()=>{
                        ElMessage.success('删除成功');
                        flowModelList();
                    })
                })
                .catch(() => {});
        }
        const handleView = (row:FlowModelTableColumns)=>{
            detailRef.value.openDialog(toRaw(row));
        }
        //导出excel
        const handleExport = ()=>{
            downLoadXml('/api/v1/flow/flowModel/export',state.tableData.param,'get')
        }
        const handleImport=()=>{
            loadExcelFlowModelRef.value.open()
        }
        return {
            proxy,
            editRef,
            detailRef,
            showAll,
            loading,
            single,
            multiple,
            word,
            queryRef,
            resetQuery,
            flowModelList,
            toggleSearch,
            flowTypeFormat,
            flow_model_type,
            //关联表数据选项
            flowFormCateOptions,
            flowBusinessCateFormat,
            flow_model_cate,
            statusFormat,
            flow_form_status,
            handleSelectionChange,
            handleAdd,
            handleUpdate,
            handleDelete,
            handleView,
            handleExport,
            handleImport,
            handleDesign,
            loadExcelFlowModelRef,
            ...toRefs(state),
        }
    }
})
</script>
<style lang="scss" scoped>
    .colBlock {
        display: block;
    }
    .colNone {
        display: none;
    }
    .ml-2{margin: 3px;}
</style>
